package com.fsnbigdata.repository;
import com.fsnbigdata.domain.PlantConservation;
import com.fsnbigdata.domain.QPlantConservation;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Specimen;
import com.fsnbigdata.domain.QSpecimen;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;

import java.awt.*;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = SpecimenRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = SpecimenRepositoryCustom.class)
@Transactional(readOnly = true)
public class SpecimenRepositoryImpl extends QueryDslRepositorySupportExt<Specimen> implements SpecimenRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    SpecimenRepositoryImpl() {
        super(Specimen.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DISTRIBUTION = "distribution";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRESERVE_LOCATION = "preserveLocation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_COORDINATE = "collectCoordinate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_ID = "organismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String INITIAL = "initial";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_NUMBER = "collectNumber";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_PERSON = "collectPerson";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ENDEMISM = "endemism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String STATUS = "status";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LATIN_NAME = "latinName";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_DATE = "collectDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COLLECT_LOCATION = "collectLocation";

    public static final String TYPE = "type";


    @Override
    public Page<Specimen> findAll(Predicate predicate, Pageable pageable) {
        QSpecimen specimen = QSpecimen.specimen;
        JPQLQuery<Specimen> query = from(specimen).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, specimen);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Specimen> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QSpecimen specimen = QSpecimen.specimen;
        JPQLQuery<Specimen> query = from(specimen);
        if(creator != null ){
            query.where(specimen.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { specimen.name, specimen.latinName, specimen.initial, specimen.collectNumber, specimen.collectLocation, specimen.collectDate, specimen.collectPerson, specimen.collectCoordinate, specimen.status, specimen.description, specimen.preserveLocation, specimen.endemism, specimen.distribution, specimen.organismId, specimen.creator,specimen.type };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, specimen.name).map(LATIN_NAME, specimen.latinName).map(INITIAL, specimen.initial).map(COLLECT_NUMBER, specimen.collectNumber).map(COLLECT_LOCATION, specimen.collectLocation).map(COLLECT_DATE, specimen.collectDate).map(COLLECT_PERSON, specimen.collectPerson).map(COLLECT_COORDINATE, specimen.collectCoordinate).map(STATUS, specimen.status).map(DESCRIPTION, specimen.description).map(PRESERVE_LOCATION, specimen.preserveLocation).map(ENDEMISM, specimen.endemism).map(DISTRIBUTION, specimen.distribution).map(ORGANISM_ID, specimen.organismId).map(CREATOR, specimen.creator).map(TYPE,specimen.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, specimen);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Specimen> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QSpecimen specimen = QSpecimen.specimen;
        JPQLQuery<Specimen> query = from(specimen);
        Path<?>[] paths = new Path<?>[] { specimen.name, specimen.latinName, specimen.initial, specimen.collectNumber, specimen.collectLocation, specimen.collectDate, specimen.collectPerson, specimen.collectCoordinate, specimen.status, specimen.description, specimen.preserveLocation, specimen.endemism, specimen.distribution, specimen.organismId, specimen.creator,specimen.type };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(specimen.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, specimen.name).map(LATIN_NAME, specimen.latinName).map(INITIAL, specimen.initial).map(COLLECT_NUMBER, specimen.collectNumber).map(COLLECT_LOCATION, specimen.collectLocation).map(COLLECT_DATE, specimen.collectDate).map(COLLECT_PERSON, specimen.collectPerson).map(COLLECT_COORDINATE, specimen.collectCoordinate).map(STATUS, specimen.status).map(DESCRIPTION, specimen.description).map(PRESERVE_LOCATION, specimen.preserveLocation).map(ENDEMISM, specimen.endemism).map(DISTRIBUTION, specimen.distribution).map(ORGANISM_ID, specimen.organismId).map(CREATOR, specimen.creator).map(TYPE,specimen.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, specimen);
    }
}
